package com.kye.express.business.freight_rates_query;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.express.utils.DateTimeUtils;
import com.kye.express.utils.StringUtils;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.SharedPreferencesConfig;
import com.kye.model.SharedPreferencesManager;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.freight_rates_query.QueryWaybillCostResultBean;
import com.kye.model.bean.freight_rates_query.QueryWaybillErrorCountModel;
import com.kye.model.bean.freight_rates_query.RequestQueryWaybillCostBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.FreightRatesQueryService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface NumberQueryActivityContract {

    interface NumberQueryActivityView extends RxView {

        void onLoaded(OpenApiResult<QueryWaybillCostResultBean> result);

        void onComplete();

        void onError(String msg);

        void onApiError(OpenApiResult<QueryWaybillCostResultBean> result);

        void onLoading();
    }

    class NumberQueryActivityViewPresenter extends BasePresenter<NumberQueryActivityContract.NumberQueryActivityView> {

        private Context mContext;

        public NumberQueryActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 查询原寄地
         */
        public void queryWaybillCost(String waybill, String queryPhone) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(FreightRatesQueryService.class)
                    .queryWaybillCost(new RequestQueryWaybillCostBean(waybill, phone, queryPhone))
                    .compose(getView().<OpenApiResult<QueryWaybillCostResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<QueryWaybillCostResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<QueryWaybillCostResultBean> result, QueryWaybillCostResultBean datas) {
                            if (datas != null) {
                                getView().onLoaded(result);
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<QueryWaybillCostResultBean> result, int code, String msg) {
                            getView().onApiError(result);
                        }
                    });
        }

        /**
         * 获取当前运单号查询次数
         */
        public QueryWaybillErrorCountModel getQueryWaybillErrorCount() {
            QueryWaybillErrorCountModel queryErrCountBean = new QueryWaybillErrorCountModel();
            Gson gson = new Gson();

            String json = SharedPreferencesManager.get(SharedPreferencesConfig.KEY_QUERY_WAYBILL_ERROR_COUNT);
            if (StringUtils.isNullOrEmpty(json)) {
                queryErrCountBean.setTime(System.currentTimeMillis());
                queryErrCountBean.setErrCount(0);
                return queryErrCountBean;
            } else {
                queryErrCountBean = gson.fromJson(json, QueryWaybillErrorCountModel.class);
                //如果时间不是今天了 则清空错误次数
                if (!DateTimeUtils.isTheSameDay(queryErrCountBean.getTime())) {
                    queryErrCountBean.setTime(System.currentTimeMillis());
                    queryErrCountBean.setErrCount(0);
                }
            }
            return queryErrCountBean;
        }

        /**
         * 存储当前运单号查询次数
         */
        public void setQueryWaybillErrorCount(QueryWaybillErrorCountModel queryErrCountBean) {
            if (queryErrCountBean != null) {
                Gson gson = new Gson();
                String beanJson = gson.toJson(queryErrCountBean);
                SharedPreferencesManager.commit(SharedPreferencesConfig.KEY_QUERY_WAYBILL_ERROR_COUNT, beanJson);
            }
        }
    }
}
